//
//  WebViewViewController.m
//  CWRD
//
//  Created by lanou on 15/9/19.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "WebViewViewController.h"
#import "DailySelectionModel.h"

@interface WebViewViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) NSMutableArray *pArray;

@end

@implementation WebViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setLeftUIBarButtonItem];
    
    self.view.backgroundColor = [UIColor whiteColor];
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:kBounds];
    scrollView.contentSize = CGSizeMake(kWidth * self.modelArray.count, 0);
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    
    for (int i = 0; i < self.modelArray.count; i++) {
        UIWebView *web = [[UIWebView alloc]initWithFrame:CGRectMake(kWidth * i, 0, kWidth, kHeight)];
        web.tag = i + 100;
        [scrollView addSubview:web];
    }
    [scrollView setContentOffset:CGPointMake(self.index * kWidth, 0) animated:YES];
    DailySelectionModel *model = self.modelArray[self.index];
    UIWebView *currentWeb = (UIWebView *)[scrollView viewWithTag:self.index + 100];
    [currentWeb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:model.rawWebUrl]]];
    [self.view addSubview:scrollView];
    
    //将当前页面的位置self.index 赋给pArray
    [self.pArray addObject:[NSString stringWithFormat:@"%ld", (long)self.index]];
    
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor cyanColor]];
    
    
}


- (void)setLeftUIBarButtonItem {
    
    UIImage *image = [UIImage imageNamed:@"Entypo_2630(0)_32"];
    
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithImage:image style:(UIBarButtonItemStyleDone) target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = leftButton;
}

- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}


//scrollView滑动结束的时候判断需不需要请求刷新
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    BOOL isSame = NO;
    NSInteger p = scrollView.contentOffset.x / kWidth;
    DailySelectionModel *model = self.modelArray[p];
    //遍历pArray,看滑到的位置之前是否滑到过
    for (int i = 0; i < self.pArray.count; i++) {
        //如果数组里面有该位置信息,表示滑到过
        if ([self.pArray[i] isEqualToString:[NSString stringWithFormat:@"%ld", p]]) {
            isSame = YES;
        }
    }
    //如果没有滑到过就请求数据
    if (isSame == NO) {
        UIWebView *currentWeb = (UIWebView *)[scrollView viewWithTag:p + 100];
        [currentWeb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:model.rawWebUrl]]];
        //再将本次位置信息存到数组
        [self.pArray addObject:[NSString stringWithFormat:@"%ld", p]];
    }

}
- (NSMutableArray *)pArray {
    if (!_pArray) {
        _pArray = [[NSMutableArray alloc] init];
    }
    return _pArray;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
